﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.FormatOutput;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;

namespace OrderSystem
{
    public partial class FormInvoiceInfo : FormTemplet
    {
        private DataTable _dtInvoiceItems = null;

        private FormAddInvoiceItem frmAddInvoiceItem;

        private FormDataAnalyze frmDataAnalyze;

        private long _nInvoiceID = -1L;

        private string _sInvoiceCode = null;

        private string _sCompanyName = null;

        private string _sCompanyLinkman = null;

        private string _sProjectName = null;

        private string _sProjectLinkman = null;

        private string _sProjectLinkmanPhone = null;

        private string _sProjectAddress = null;

        private string _sInvoiceDate = null;

        private bool _isLocked = true;

        private bool _isCancelled = false;

        private string _sRemark = null;

        private string _sOperatorName = null;

        private string _sOriginalQuantity = "0";

        public FormInvoiceInfo()
        {
            this.InitializeComponent();
        }

        public FormInvoiceInfo(string sInvoiceCode) : this()
        {
            this._sInvoiceCode = sInvoiceCode;
            this.RefreshInvoice();
            this.RefreshItems();
            for (int i = 0; i < this.dgInvoice.Columns.Count; i++)
            {
                this.dgInvoice.Columns[i].ReadOnly = true;
            }
            if (!LoginInfo.Role[PowerName.InvoiceData_Update])
            {
                this.dgInvoice.Columns[8].ReadOnly = true;
            }
            else
            {
                this.dgInvoice.Columns[8].ReadOnly = false;
            }
        }

        private void RefreshInvoice()
        {
            DataTable dataTable = null;
            Invoices invoices = new Invoices();
            try
            {
                dataTable = invoices.LoadInvoiceByInvoiceCode(this._sInvoiceCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            invoices.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this._nInvoiceID = (long)Convert.ToInt32(dataTable.Rows[0]["发货单序号"]);
                    this._sCompanyName = dataTable.Rows[0]["公司名称"].ToString();
                    this._sCompanyLinkman = dataTable.Rows[0]["公司联系人"].ToString();
                    this._sProjectName = dataTable.Rows[0]["计划名称"].ToString();
                    this._sProjectLinkman = dataTable.Rows[0]["计划联系人"].ToString();
                    this._sProjectLinkmanPhone = dataTable.Rows[0]["计划联系人电话"].ToString();
                    this._sProjectAddress = dataTable.Rows[0]["计划地址"].ToString();
                    this._sInvoiceDate = dataTable.Rows[0]["发货日期"].ToString();
                    this._isLocked = Convert.ToBoolean(dataTable.Rows[0]["是否锁定"]);
                    this._isCancelled = Convert.ToBoolean(dataTable.Rows[0]["是否作废"]);
                    this._sRemark = dataTable.Rows[0]["发货单备注"].ToString();
                    this._sOperatorName = dataTable.Rows[0]["操作者"].ToString();
                    this.txtInvoiceNumber.Text = this._sInvoiceCode;
                    this.txtCompanyName.Text = this._sCompanyName + "(" + this._sCompanyLinkman + ")";
                    this.txtPorjectName.Text = this._sProjectName + "(" + this._sProjectLinkman + ")";
                    this.txtProjectAddress.Text = this._sProjectAddress + "(" + this._sProjectLinkmanPhone + ")";
                    this.txtInvoiceDate.Text = this._sInvoiceDate;
                    this.chkLockedData.Checked = this._isLocked;
                    this.chkCancelled.Checked = this._isCancelled;
                    this.txtRemark.Text = this._sRemark;
                    this.txtOperatorName.Text = this._sOperatorName;
                }
                else
                {
                    MessageBox.Show("没有查询到此订单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
        }

        private void RefreshItems()
        {
            if (this._dtInvoiceItems != null)
            {
                this._dtInvoiceItems.Dispose();
                this._dtInvoiceItems = null;
            }
            InvoiceItems invoiceItems = new InvoiceItems();
            try
            {
                this._dtInvoiceItems = invoiceItems.GetInvoiceItems(this._nInvoiceID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            invoiceItems.Dispose();
            if (this._dtInvoiceItems != null)
            {
                this.dgInvoice.DataSource = this._dtInvoiceItems;
                this.dgInvoice.AutoResizeColumns();
                bool flag = false;
                for (int i = 0; i < this._dtInvoiceItems.Rows.Count; i++)
                {
                    if (this._dtInvoiceItems.Rows[i]["宽(短边)"].ToString().Length > 0 || this._dtInvoiceItems.Rows[i]["高(短边)"].ToString().Length > 0)
                    {
                        flag = true;
                        this.dgInvoice.Columns["宽(短边)"].Visible = true;
                        this.dgInvoice.Columns["高(短边)"].Visible = true;
                        break;
                    }
                }
                if (!flag)
                {
                    this.dgInvoice.Columns["宽(短边)"].Visible = false;
                    this.dgInvoice.Columns["高(短边)"].Visible = false;
                }
            }
            else
            {
                MessageBox.Show("加载数据失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void ReloadInvoiceItemsForInsert(string sProductID)
        {
            if (this.menuShowData.Checked)
            {
                if (this._dtInvoiceItems == null)
                {
                    MessageBox.Show("数据容器是空，可能还有数据保存在数据库，但没有加载到容器中，你可以从新打开此发货单试一试，但是现有数据将加载在此容器中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this._dtInvoiceItems = new DataTable("InvoiceItem");
                    this._dtInvoiceItems.Columns.Add(new DataColumn("产品ID", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("类型", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("单位", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("编号", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("宽", typeof(double)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("宽(短边)", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("高", typeof(double)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("高(短边)", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("数量", typeof(int)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("面积", typeof(double)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("价格", typeof(double)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("备注", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("工艺", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("客户", typeof(string)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("计划ID", typeof(int)));
                    this._dtInvoiceItems.Columns.Add(new DataColumn("计划", typeof(string)));
                }
                DataTable dataTable = null;
                InvoiceItems invoiceItems = new InvoiceItems();
                try
                {
                    dataTable = invoiceItems.LoadInvoiceItem(this._nInvoiceID, sProductID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                invoiceItems.Dispose();
                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                    {
                        bool flag = false;
                        for (int i = 0; i < this._dtInvoiceItems.Rows.Count; i++)
                        {
                            if (this._dtInvoiceItems.Rows[i][0].ToString().ToUpper() == sProductID.ToUpper())
                            {
                                flag = true;
                                this._dtInvoiceItems.Rows[i][8] = dataTable.Rows[0][8];
                                decimal num = Convert.ToDecimal(dataTable.Rows[0][4]) * Convert.ToDecimal(dataTable.Rows[0][6]) / 1000000m;
                                this._dtInvoiceItems.Rows[i][9] = Convert.ToDouble(Convert.ToDecimal(num.ToString("F4")) * Convert.ToInt32(dataTable.Rows[0][8]));
                                this._dtInvoiceItems.Rows[0][10] = Convert.ToDouble(this._dtInvoiceItems.Rows[0][10]);
                                break;
                            }
                        }
                        if (!flag)
                        {
                            DataRow dataRow = this._dtInvoiceItems.NewRow();
                            dataRow[0] = dataTable.Rows[0][0];
                            dataRow[1] = dataTable.Rows[0][1];
                            dataRow[2] = dataTable.Rows[0][2];
                            dataRow[3] = dataTable.Rows[0][3];
                            dataRow[4] = dataTable.Rows[0][4];
                            dataRow[5] = dataTable.Rows[0][5];
                            dataRow[6] = dataTable.Rows[0][6];
                            dataRow[7] = dataTable.Rows[0][7];
                            dataRow[8] = dataTable.Rows[0][8];
                            dataRow[9] = dataTable.Rows[0][9];
                            dataRow[10] = Convert.ToDouble(dataTable.Rows[0][10]);
                            dataRow[11] = dataTable.Rows[0][11];
                            dataRow[12] = dataTable.Rows[0][12];
                            dataRow[13] = dataTable.Rows[0][13];
                            dataRow[14] = dataTable.Rows[0][14];
                            dataRow[15] = dataTable.Rows[0][15];
                            this._dtInvoiceItems.Rows.Add(dataRow);
                        }
                        this._dtInvoiceItems.AcceptChanges();
                        for (int i = 0; i < this._dtInvoiceItems.Rows.Count; i++)
                        {
                            if (this.dgInvoice.Rows[i].Cells[0].Value.ToString() == sProductID)
                            {
                                this.dgInvoice.CurrentCell = this.dgInvoice.Rows[i].Cells[0];
                                this.dgInvoice.Rows[i].Selected = true;
                                break;
                            }
                        }
                        this.dgInvoice.AutoResizeColumns();
                    }
                    else
                    {
                        MessageBox.Show("没有查询到此项数据！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    dataTable.Dispose();
                }
            }
        }

        private void FormInvoiceInfo_Load(object sender, EventArgs e)
        {
            this.dgInvoice.AutoResizeColumns();
            if (!LoginInfo.Role[PowerName.InvoiceData_SeePrice])
            {
                if (this.dgInvoice != null)
                {
                    this.dgInvoice.Columns["价格"].Visible = false;
                }
            }
        }

        public void ImportData(DataTable dtImportData)
        {
            if (this._nInvoiceID != -1L)
            {
                InvoiceItems invoiceItems = new InvoiceItems();
                int i = 0;
                while (i < dtImportData.Rows.Count)
                {
                    bool flag = false;
                    try
                    {
                        flag = invoiceItems.InsertInvoiceItem(this._nInvoiceID, dtImportData.Rows[i][1].ToString(), Convert.ToInt32(dtImportData.Rows[i][9]), LoginInfo.OperatorID);
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 50401 || ex.Number == 50402 || ex.Number == 50200 || ex.Number == 50000)
                        {
                            dtImportData.Rows[i][0] = ex.Message;
                            dtImportData.AcceptChanges();
                        }
                        else
                        {
                            MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    if (flag)
                    {
                        DataRow dataRow = this._dtInvoiceItems.NewRow();
                        dataRow[0] = dtImportData.Rows[i][1].ToString();
                        dataRow[1] = dtImportData.Rows[i][2].ToString();
                        dataRow[2] = dtImportData.Rows[i][3].ToString();
                        dataRow[3] = dtImportData.Rows[i][4].ToString();
                        dataRow[4] = dtImportData.Rows[i][5].ToString();
                        dataRow[5] = dtImportData.Rows[i][6].ToString();
                        dataRow[6] = dtImportData.Rows[i][7].ToString();
                        dataRow[7] = dtImportData.Rows[i][8].ToString();
                        dataRow[8] = dtImportData.Rows[i][9].ToString();
                        dataRow[9] = dtImportData.Rows[i][10].ToString();
                        dataRow[10] = dtImportData.Rows[i][11].ToString();
                        dataRow[11] = dtImportData.Rows[i][12].ToString();
                        dataRow[12] = dtImportData.Rows[i][13].ToString();
                        dataRow[13] = dtImportData.Rows[i][14].ToString();
                        dataRow[14] = dtImportData.Rows[i][15].ToString();
                        dataRow[15] = dtImportData.Rows[i][16].ToString();
                        this._dtInvoiceItems.Rows.Add(dataRow);
                        this._dtInvoiceItems.AcceptChanges();
                        dtImportData.Rows[i].Delete();
                        dtImportData.AcceptChanges();
                    }
                    else
                    {
                        i++;
                    }
                }
                invoiceItems.Dispose();
                this.dgInvoice.AutoResizeColumns();
            }
        }

        private void lkUpdateRemark_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能更改备注！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                string text = InputBox.ShowBox("请输入你要更改的备注...", this.txtRemark.Text);
                if (text != null)
                {
                    Invoices invoices = new Invoices();
                    bool flag = false;
                    try
                    {
                        flag = invoices.UpdateRemark(this._sInvoiceCode, text.Trim());
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    invoices.Dispose();
                    if (flag)
                    {
                        MessageBox.Show("更改备注成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this._sRemark = text.Trim();
                        this.txtRemark.Text = this._sRemark;
                    }
                }
            }
        }

        private void btnAddInvoiceOrder_Click(object sender, EventArgs e)
        {
            this.menuAddInvoiceOrder_Click(null, null);
        }

        private void menuAddInvoiceOrder_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.InvoiceData_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.frmDataAnalyze != null && !this.frmDataAnalyze.IsDisposed)
            {
                MessageBox.Show("处理完一组数据之后才能处理下一组数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.frmDataAnalyze.Show();
                this.frmDataAnalyze.Activate();
            }
            else
            {
                string text = InputBox.ShowBox("请输入你要出库的订单号...", "");
                if (text != null)
                {
                    OrderItems orderItems = new OrderItems();
                    DataTable dataTable = null;
                    try
                    {
                        dataTable = orderItems.GetProductIDCollection(text.Trim());
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n获取出库信息可能出现了错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    orderItems.Dispose();
                    if (dataTable != null)
                    {
                        if (dataTable.Rows.Count > 0)
                        {
                            Hashtable hashtable = new Hashtable();
                            for (int i = 0; i < dataTable.Rows.Count; i++)
                            {
                                if (Convert.ToInt32(dataTable.Rows[i][1]) > Convert.ToInt32(dataTable.Rows[i][2]))
                                {
                                    hashtable.Add(dataTable.Rows[i][0].ToString(), Convert.ToInt32(dataTable.Rows[i][1]) - Convert.ToInt32(dataTable.Rows[i][2]));
                                }
                            }
                            if (hashtable.Count > 0)
                            {
                                this.frmDataAnalyze = new FormDataAnalyze(hashtable, this._sInvoiceCode, this);
                                this.frmDataAnalyze.MdiParent = base.MdiParent;
                                this.frmDataAnalyze.Show();
                                this.ImportData(this.frmDataAnalyze.ImportData);
                                this.frmDataAnalyze.dgImportData.AutoResizeColumns();
                            }
                            else
                            {
                                MessageBox.Show("此订单的数据已经全部发货 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            MessageBox.Show("此订单号没有数据，或者是订单号输入错误，请检查 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    if (this.frmDataAnalyze != null && this.frmDataAnalyze.ImportData.Rows.Count == 0)
                    {
                        this.frmDataAnalyze.Close();
                        this.frmDataAnalyze = null;
                    }
                }
            }
        }

        private void menuAddInvoiceFile_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.InvoiceData_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.frmDataAnalyze != null && !this.frmDataAnalyze.IsDisposed)
            {
                MessageBox.Show("处理完一组数据之后才能处理下一组数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.frmDataAnalyze.Show();
                this.frmDataAnalyze.Activate();
            }
            else
            {
                Hashtable hashtable = new Hashtable();
                this.openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                this.openFileDialog1.Title = "请选择你要导入的文件";
                this.openFileDialog1.FileName = "";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        StreamReader streamReader = new StreamReader(File.Open(this.openFileDialog1.FileName, FileMode.Open), Encoding.Default);
                        this.openFileDialog1.Dispose();
                        int num = 0;
                        string text;
                        while ((text = streamReader.ReadLine()) != null)
                        {
                            num++;
                            if (text.Trim() != "")
                            {
                                string[] array = text.Split(new char[]
                                {
                                    ','
                                });
                                if (array.Length != 2 || !Regex.IsMatch(array[1].Trim(), "^[0-9]{1,}$") || Convert.ToInt32(array[1].Trim()) <= 0)
                                {
                                    MessageBox.Show("第" + num.ToString() + "行数据格式有问题", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    streamReader.Close();
                                    return;
                                }
                                hashtable.Add(array[0].Trim(), array[1].Trim());
                            }
                        }
                        streamReader.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (hashtable.Count > 0)
                    {
                        this.frmDataAnalyze = new FormDataAnalyze(hashtable, this._sInvoiceCode, this);
                        this.frmDataAnalyze.MdiParent = base.MdiParent;
                        this.frmDataAnalyze.Show();
                        this.ImportData(this.frmDataAnalyze.ImportData);
                        this.frmDataAnalyze.dgImportData.AutoResizeColumns();
                    }
                    else
                    {
                        MessageBox.Show("此订单没有数据 或者是 此订单号输入错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                if (this.frmDataAnalyze != null && this.frmDataAnalyze.ImportData.Rows.Count == 0)
                {
                    this.frmDataAnalyze.Close();
                    this.frmDataAnalyze = null;
                }
            }
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            this.menuAddInvoiceItem_Click(null, null);
        }

        private void menuAddInvoiceItem_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.InvoiceData_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddInvoiceItem == null || this.frmAddInvoiceItem.IsDisposed)
                {
                    this.frmAddInvoiceItem = new FormAddInvoiceItem(this._nInvoiceID, this);
                    this.frmAddInvoiceItem.MdiParent = base.MdiParent;
                }
                this.frmAddInvoiceItem.Show();
                this.frmAddInvoiceItem.Activate();
            }
        }

        private void btnUpdateItem_Click(object sender, EventArgs e)
        {
            this.menuUpdateInvoiceItem_Click(null, null);
        }

        private void menuUpdateInvoiceItem_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.InvoiceData_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgInvoice.Rows[0].Cells[0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgInvoice.CurrentCell.RowIndex;
                string text2 = InputBox.ShowBox("请输入你要改正的数量...", this.dgInvoice.Rows[rowIndex].Cells[8].Value.ToString());
                if (text2 != null)
                {
                    text2 = text2.Trim();
                    if (!Regex.IsMatch(text2, "^[0-9]{1,}$") || Convert.ToInt32(text2) < 1)
                    {
                        MessageBox.Show("请输入正确的数字！（注：必须是大于0的正整数）", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        InvoiceItems invoiceItems = new InvoiceItems();
                        bool flag = false;
                        try
                        {
                            flag = invoiceItems.UpdateInvoiceItem(this._nInvoiceID, this.dgInvoice.Rows[rowIndex].Cells[0].Value.ToString(), Convert.ToInt32(text2), LoginInfo.OperatorID);
                        }
                        catch (SqlException ex)
                        {
                            int number = ex.Number;
                            if (number != 50001)
                            {
                                switch (number)
                                {
                                    case 50300:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        this.chkLockedData.Checked = true;
                                        break;
                                    case 50301:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        break;
                                    default:
                                        switch (number)
                                        {
                                            case 50400:
                                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                goto IL_273;
                                            case 50402:
                                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                goto IL_273;
                                        }
                                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                        break;
                                }
                            }
                            else
                            {
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        IL_273:;
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        invoiceItems.Dispose();
                        if (flag)
                        {
                            MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            for (int i = 0; i < this._dtInvoiceItems.Rows.Count; i++)
                            {
                                if (this._dtInvoiceItems.Rows[i][0].ToString() == this.dgInvoice.Rows[rowIndex].Cells[0].Value.ToString())
                                {
                                    this._dtInvoiceItems.Rows[i][8] = Convert.ToInt32(text2);
                                    decimal num = Convert.ToDecimal(this._dtInvoiceItems.Rows[i][4]) * Convert.ToDecimal(this._dtInvoiceItems.Rows[i][6]) / 1000000m;
                                    this._dtInvoiceItems.Rows[i][9] = Convert.ToDouble(Convert.ToDecimal(num.ToString("F4")) * Convert.ToInt32(text2));
                                    this._dtInvoiceItems.Rows[0][10] = Convert.ToDouble(this._dtInvoiceItems.Rows[0][10]);
                                    this._dtInvoiceItems.AcceptChanges();
                                    this.dgInvoice.CurrentCell = this.dgInvoice.Rows[rowIndex].Cells[0];
                                    this.dgInvoice.Rows[rowIndex].Selected = true;
                                    break;
                                }
                            }
                            this.dgInvoice.AutoResizeColumns();
                        }
                    }
                }
            }
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            this.menuDeleteInvoiceItem_Click(null, null);
        }

        private void dgInvoice_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.menuDeleteInvoiceItem_Click(null, null);
            }
        }

        private void menuDeleteInvoiceItem_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.InvoiceData_Delete])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.chkLockedData.Checked)
            {
                MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgInvoice.Rows.Count < 1)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (MessageBox.Show("是否真的要删除这些选中的数据吗 ？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Cancel)
            {
                InvoiceItems invoiceItems = new InvoiceItems();
                try
                {
                    ArrayList arrayList = new ArrayList();
                    for (int i = 0; i < this.dgInvoice.SelectedRows.Count; i++)
                    {
                        arrayList.Add(this.dgInvoice.SelectedRows[i].Cells[0].Value.ToString());
                    }
                    for (int i = 0; i < arrayList.Count; i++)
                    {
                        invoiceItems.DeleteInvoiceItem(this._nInvoiceID, arrayList[i].ToString(), LoginInfo.OperatorID);
                        for (int j = 0; j < this._dtInvoiceItems.Rows.Count; j++)
                        {
                            if (this._dtInvoiceItems.Rows[j][0].ToString() == arrayList[i].ToString())
                            {
                                DataRow dataRow = this._dtInvoiceItems.Rows[j];
                                dataRow.Delete();
                                dataRow.AcceptChanges();
                                break;
                            }
                        }
                    }
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number != 50001)
                    {
                        switch (number)
                        {
                            case 50300:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.chkLockedData.Checked = true;
                                break;
                            case 50301:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                break;
                            default:
                                if (number != 50400)
                                {
                                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                break;
                        }
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoiceItems.Dispose();
            }
        }

        private void menuRefreshInvoice_Click(object sender, EventArgs e)
        {
            this.RefreshInvoice();
        }

        private void menuRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshItems();
        }

        private void btnCreateInvoice_Click(object sender, EventArgs e)
        {
            this.menuCreateInvoice_Click(null, null);
        }

        private void menuCreateInvoice_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Print])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!this._isLocked)
            {
                MessageBox.Show("此单需要 锁定 后方能生成发货单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this._nInvoiceID == -1L)
            {
                MessageBox.Show("加载数据时出现了错误，不能生成发货单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Invoices invoices = new Invoices();
                int num = -2;
                try
                {
                    num = invoices.GetPrintedQuantity(this._sInvoiceCode);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoices.Dispose();
                if (num > -1)
                {
                    if (num > 0)
                    {
                        if (MessageBox.Show("此单已经生成了 " + num.ToString() + " 次，是否继续此次打印？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            return;
                        }
                    }
                }
                else if (num == -1)
                {
                    MessageBox.Show("没有找到此单！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                DataSet dataSet = null;
                InvoiceItems invoiceItems = new InvoiceItems();
                try
                {
                    dataSet = invoiceItems.GetDataForInvoiceForm(this._sInvoiceCode);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoiceItems.Dispose();
                if (dataSet != null)
                {
                    if (dataSet.Tables.Count != 2 || dataSet.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("没有找到数据信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        dataSet.Dispose();
                    }
                    else
                    {
                        string text = null;
                        try
                        {
                            this.saveFileDialog1.Filter = "Excel文件|*.xls";
                            this.saveFileDialog1.Title = "请选择你要输入的路径和文件名";
                            this.saveFileDialog1.FileName = this._sInvoiceCode + ".xls";
                            this.saveFileDialog1.InitialDirectory = AppSettings.GetAppSettings("exportInvoicePath");
                            if (this.saveFileDialog1.ShowDialog() != DialogResult.OK)
                            {
                                this.saveFileDialog1.Dispose();
                                dataSet.Dispose();
                                return;
                            }
                            text = this.saveFileDialog1.FileName;
                            this.saveFileDialog1.Dispose();
                            File.Copy(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("invoice"), text, true);
                        }
                        catch (IOException ex3)
                        {
                            MessageBox.Show(ex3.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dataSet.Dispose();
                            return;
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dataSet.Dispose();
                            return;
                        }
                        invoices = new Invoices();
                        bool flag = false;
                        try
                        {
                            flag = invoices.PrintOnce(this._sInvoiceCode);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        invoices.Dispose();
                        if (flag)
                        {
                            Orders orders = new Orders();
                            string text2 = null;
                            try
                            {
                                text2 = orders.GetOrderCode(this._sInvoiceCode);
                            }
                            catch (SqlException ex)
                            {
                                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            orders.Dispose();
                            try
                            {
                                ExcelOperationInvoice excelOperationInvoice = new ExcelOperationInvoice(Application.StartupPath);
                                excelOperationInvoice.MakeInvoiceDataToExcel(text, dataSet, this._sInvoiceCode, this._sCompanyName, this._sProjectName, DateTime.Parse(this._sInvoiceDate).ToShortDateString(), this._sProjectLinkman, this._sProjectLinkmanPhone, this._sProjectAddress, this._sRemark, LoginInfo.OperatorName, (text2 != null) ? text2 : "");
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            dataSet.Dispose();
                        }
                    }
                }
            }
        }

        private void menuCreateInvoiceSumup_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Print])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!this._isLocked)
            {
                MessageBox.Show("此单需要 锁定 后方能生成发货单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                this.saveFileDialog1.Filter = "Excel文件|*.xls";
                this.saveFileDialog1.Title = "请选择你要输入的路径和文件名";
                this.saveFileDialog1.FileName = this._sInvoiceCode + "_总结.xls";
                string text = null;
                if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    text = this.saveFileDialog1.FileName;
                    this.saveFileDialog1.Dispose();
                    try
                    {
                        File.Copy(Application.StartupPath + "\\model\\InvoiceBill.xls", text, true);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    Invoices invoices = new Invoices();
                    DataSet dataSet = null;
                    try
                    {
                        dataSet = invoices.GetTypeSubtotalOfInvoice(this._sInvoiceCode);
                    }
                    catch (SqlException ex3)
                    {
                        MessageBox.Show(ex3.Message + "\r\n\r\n数据库出现了错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    invoices.Dispose();
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count == 2)
                        {
                            try
                            {
                                ExcelOperationInvoice2 excelOperationInvoice = new ExcelOperationInvoice2(Application.StartupPath);
                                excelOperationInvoice.MakeDataToExcel(text, dataSet, this._sInvoiceCode, this._sCompanyName, this._sProjectName, DateTime.Parse(this._sInvoiceDate).ToShortDateString(), this._sProjectLinkman, this._sProjectLinkmanPhone, this._sProjectAddress, this._sOperatorName);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            MessageBox.Show("发货单可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    if (dataSet != null)
                    {
                        dataSet.Dispose();
                    }
                }
                else
                {
                    this.saveFileDialog1.Dispose();
                }
            }
        }

        private void menuExportData_Click(object sender, EventArgs e)
        {
            this._dtInvoiceItems.AcceptChanges();
            this.saveFileDialog1.Filter = "xml 文件|*.xml";
            this.saveFileDialog1.Title = "请选择你要输入的路径和文件名";
            this.saveFileDialog1.FileName = this._sInvoiceCode + ".xml";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.saveFileDialog1.FileName;
                this.saveFileDialog1.Dispose();
                try
                {
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName, Encoding.UTF8);
                    xmlTextWriter.WriteStartDocument(false);
                    xmlTextWriter.WriteStartElement("data");
                    xmlTextWriter.WriteAttributeString("type", "invoice");
                    xmlTextWriter.WriteStartElement("invoice");
                    xmlTextWriter.WriteAttributeString("invoiceNumber", this._sInvoiceCode);
                    xmlTextWriter.WriteAttributeString("company", this._sCompanyName);
                    xmlTextWriter.WriteAttributeString("date", this._sInvoiceDate);
                    xmlTextWriter.WriteAttributeString("remark", this._sRemark);
                    for (int i = 0; i < this._dtInvoiceItems.Rows.Count; i++)
                    {
                        xmlTextWriter.WriteStartElement("item");
                        xmlTextWriter.WriteAttributeString("serial", this._dtInvoiceItems.Rows[i][0].ToString());
                        xmlTextWriter.WriteAttributeString("type", this._dtInvoiceItems.Rows[i][1].ToString());
                        xmlTextWriter.WriteAttributeString("ProductCode", this._dtInvoiceItems.Rows[i][3].ToString());
                        xmlTextWriter.WriteAttributeString("width", this._dtInvoiceItems.Rows[i][4].ToString());
                        xmlTextWriter.WriteAttributeString("Height", this._dtInvoiceItems.Rows[i][6].ToString());
                        xmlTextWriter.WriteAttributeString("quantity", this._dtInvoiceItems.Rows[i][8].ToString());
                        xmlTextWriter.WriteAttributeString("area", this._dtInvoiceItems.Rows[i][9].ToString());
                        xmlTextWriter.WriteAttributeString("price", this._dtInvoiceItems.Rows[i][10].ToString());
                        xmlTextWriter.WriteAttributeString("remark", this._dtInvoiceItems.Rows[i][11].ToString());
                        xmlTextWriter.WriteEndElement();
                    }
                    xmlTextWriter.WriteEndElement();
                    xmlTextWriter.WriteEndElement();
                    xmlTextWriter.WriteEndDocument();
                    xmlTextWriter.Flush();
                    xmlTextWriter.Close();
                }
                catch (XmlException ex)
                {
                    MessageBox.Show(ex.Message, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                this.saveFileDialog1.Dispose();
            }
        }

        private void chkLockedData_Click(object sender, EventArgs e)
        {
            if (this.chkLockedData.Checked)
            {
                this.menuUnLockData_Click(null, null);
            }
            else
            {
                this.menuLockData_Click(null, null);
            }
        }

        private void menuLockData_Click(object sender, EventArgs e)
        {
            Invoices invoices = new Invoices();
            bool flag = false;
            try
            {
                flag = invoices.LockData(this._sInvoiceCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            invoices.Dispose();
            if (flag)
            {
                this._isLocked = true;
                this.chkLockedData.Checked = true;
            }
        }

        private void menuUnLockData_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_UnLock])
            {
                MessageBox.Show("你没有权限操作这里", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Invoices invoices = new Invoices();
                bool flag = false;
                try
                {
                    flag = invoices.UnLockData(this._sInvoiceCode);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoices.Dispose();
                if (flag)
                {
                    this._isLocked = false;
                    this.chkLockedData.Checked = false;
                }
            }
        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Cancel])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Invoices invoices = new Invoices();
                bool flag = false;
                try
                {
                    flag = invoices.Cancel(this._sInvoiceCode);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoices.Dispose();
                if (flag)
                {
                    this._isCancelled = true;
                    this.chkCancelled.Checked = true;
                }
            }
        }

        private void menuActivate_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Activate])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Invoices invoices = new Invoices();
                bool flag = false;
                try
                {
                    flag = invoices.Activate(this._sInvoiceCode);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                invoices.Dispose();
                if (flag)
                {
                    this._isCancelled = false;
                    this.chkCancelled.Checked = false;
                }
            }
        }

        private void menuShowSubtenseColumn_Click(object sender, EventArgs e)
        {
            if (this._dtInvoiceItems != null)
            {
                if (this.dgInvoice.Columns["宽(短边)"].Visible)
                {
                    this.dgInvoice.Columns["宽(短边)"].Visible = false;
                    this.dgInvoice.Columns["高(短边)"].Visible = false;
                }
                else
                {
                    this.dgInvoice.Columns["宽(短边)"].Visible = true;
                    this.dgInvoice.Columns["高(短边)"].Visible = true;
                }
            }
        }

        private void menuShowData_Click(object sender, EventArgs e)
        {
            if (this.menuShowData.Checked)
            {
                this.menuShowData.Checked = false;
                this.menuShowData.Text = "暂时不显示所输入的数据";
            }
            else
            {
                this.menuShowData.Checked = true;
                this.menuShowData.Text = "显示所输入的数据";
            }
        }

        private void menuTypeSortArea_Click(object sender, EventArgs e)
        {
            Invoices invoices = new Invoices();
            DataSet dataSet = null;
            try
            {
                dataSet = invoices.GetTypeSubtotalOfInvoice(this._sInvoiceCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            invoices.Dispose();
            if (dataSet != null)
            {
                if (dataSet.Tables.Count == 2)
                {
                    string text = "※ 发货单的分类信息如下：\r\n";
                    int num = 0;
                    decimal d = 0m;
                    decimal d2 = 0m;
                    decimal d3 = 0m;
                    string text2;
                    for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                    {
                        int num2 = Convert.ToInt32(dataSet.Tables[0].Rows[i][1]);
                        num += num2;
                        decimal num3 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][2]);
                        d += num3;
                        decimal d4 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][3]);
                        d2 += d4;
                        decimal d5 = Convert.ToDecimal(dataSet.Tables[0].Rows[i][4]);
                        decimal d6 = Convert.ToDecimal((num3 * d5).ToString("F4"));
                        d3 += d6;
                        text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、类型：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][0].ToString(), 16),
                            "数量：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][1].ToString(), 10),
                            "面积：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][2].ToString(), 16),
                            "周长：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[0].Rows[i][3].ToString(), 16),
                            LoginInfo.Role[PowerName.InvoiceData_SeePrice] ? ("价格：" + dataSet.Tables[0].Rows[i][4].ToString()) : ""
                        });
                    }
                    text2 = text;
                    text = string.Concat(new string[]
                    {
                        text2,
                        "\r\n\r\n     ★数量为：",
                        num.ToString(),
                        "；   总平方为：",
                        d.ToString(),
                        "；   总周长为：",
                        d2.ToString(),
                        LoginInfo.Role[PowerName.InvoiceData_SeePrice] ? ("；   金额为：" + d3.ToString()) : ""
                    });
                    text += "\r\n\r\n※ 工艺分类信息如下：\r\n";
                    decimal d7 = 0m;
                    decimal num4 = 0m;
                    for (int i = 0; i < dataSet.Tables[1].Rows.Count; i++)
                    {
                        d7 += Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]);
                        num4 = Convert.ToDecimal((num4 + Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]) * Convert.ToDecimal(dataSet.Tables[1].Rows[i][2])).ToString("F4"));
                        text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、工艺：",
                            CommonMethod.PadSpaceOnRight(dataSet.Tables[1].Rows[i][0].ToString(), 16),
                            "数量：",
                            CommonMethod.PadSpaceOnRight(Convert.ToDecimal(dataSet.Tables[1].Rows[i][1]).ToString(), 16),
                            LoginInfo.Role[PowerName.InvoiceData_SeePrice] ? ("价格：" + dataSet.Tables[1].Rows[i][2].ToString()) : ""
                        });
                    }
                    text = text + "\r\n\r\n     ★加工工艺的数量为：" + d7.ToString() + (LoginInfo.Role[PowerName.InvoiceData_SeePrice] ? ("；   加工工艺的金额为：" + num4.ToString()) : "");
                    if (LoginInfo.Role[PowerName.InvoiceData_SeePrice])
                    {
                        text = text + "\r\n\r\n◆ 总金额为：" + (d3 + num4).ToString("F2");
                    }
                    MessageBox.Show(text, "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("发货单可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            if (dataSet != null)
            {
                dataSet.Dispose();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void dgInvoice_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            this._sOriginalQuantity = this.dgInvoice.Rows[e.RowIndex].Cells[8].Value.ToString().Trim();
        }

        private void dgInvoice_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                string text = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (!(this._sOriginalQuantity == text))
                {
                    if (!LoginInfo.Role[PowerName.InvoiceData_Update])
                    {
                        MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                        this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    else if (this.chkLockedData.Checked)
                    {
                        MessageBox.Show("此发货单已经锁定，不能进行编辑", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                        this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    else if (text == null || !Regex.IsMatch(text, "^[0-9]{1,8}$") || Convert.ToInt32(text) < 0)
                    {
                        MessageBox.Show("你所输入的 数量 格式不正确，此处应该为 零 或 正整数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                        this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    else
                    {
                        InvoiceItems invoiceItems = new InvoiceItems();
                        bool flag = false;
                        try
                        {
                            flag = invoiceItems.UpdateInvoiceItem(this._nInvoiceID, this.dgInvoice.Rows[e.RowIndex].Cells[0].Value.ToString(), Convert.ToInt32(text), LoginInfo.OperatorID);
                        }
                        catch (SqlException ex)
                        {
                            int number = ex.Number;
                            if (number != 50001)
                            {
                                switch (number)
                                {
                                    case 50300:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        this.chkLockedData.Checked = true;
                                        break;
                                    case 50301:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        break;
                                    default:
                                        switch (number)
                                        {
                                            case 50400:
                                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                goto IL_357;
                                            case 50402:
                                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                goto IL_357;
                                        }
                                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                        break;
                                }
                            }
                            else
                            {
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        IL_357:
                            this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                            this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                            this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        invoiceItems.Dispose();
                        if (flag)
                        {
                            decimal num = Convert.ToDecimal(this.dgInvoice.Rows[e.RowIndex].Cells[4].Value) * Convert.ToDecimal(this.dgInvoice.Rows[e.RowIndex].Cells[6].Value) / 1000000m;
                            this.dgInvoice.Rows[e.RowIndex].Cells[9].Value = Convert.ToDouble(Convert.ToDecimal(num.ToString("F4")) * Convert.ToInt32(text));
                            this.dgInvoice.AutoResizeColumns();
                        }
                    }
                }
            }
        }

        private void dgInvoice_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgInvoice_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("输入的格式不正确，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            if (this.chkLockedData.Checked)
            {
                this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalQuantity;
                this.dgInvoice.CurrentCell = this.dgInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }
    }
}
